﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace LambdaManAi
{
    public class LambdaManAICompiler
    {
        private readonly LabelGenerator _labelGenerator = new LabelGenerator();

        public void CreateFunction(string functionName, Expression expression)
        {
            Console.WriteLine(functionName + ":");
            var expressionVisitor = new CompilerExpressionVisitor(_labelGenerator);
            expressionVisitor.Visit(expression);
            Console.WriteLine("RTN");
            Console.WriteLine();
            expressionVisitor.WriteSelectBranches();
        }

        public void CreateExtenderFunction(string currentFunctionName, string nextFunctionName, int currentFunctionArgsCount, Expression localBody)
        {
            Console.WriteLine(currentFunctionName + ":");
            for (int i = 0; i < currentFunctionArgsCount; i++)
                Console.WriteLine("LD 0 {0}", i);
            var expressionVisitor = new CompilerExpressionVisitor(_labelGenerator);
            expressionVisitor.Visit(localBody);
            Console.WriteLine("LDF {0}", nextFunctionName);
            Console.WriteLine("AP {0}", currentFunctionArgsCount + 1);
            Console.WriteLine("RTN");
            Console.WriteLine();
            expressionVisitor.WriteSelectBranches();
        }
    }
}
